# -*- utf-8 -*-
from rest_framework import serializers

from .models import Need, Tags, Images, Video
from ..baseData.serializers import UserSerializer
from ..fontend.serializers import ClassifySerializer


class ImagesSerializer(serializers.ModelSerializer):
    class Meta:
        model = Images
        fields = '__all__'


class VideoSerializer(serializers.ModelSerializer):
    class Meta:
        model = Video
        fields = '__all__'


class TagsSerializer(serializers.ModelSerializer):
    """标记只读 name"""

    class Meta:
        model = Tags
        fields = ['name']


class NeedSerializer(serializers.ModelSerializer):
    """帖子的序列化器，外键有很多"""
    images = ImagesSerializer(allow_null=True, many=True)
    video = VideoSerializer(allow_null=True, many=True)
    user = UserSerializer()
    create_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M:%S', read_only=True)
    update_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M:%S', read_only=True)
    classify = ClassifySerializer()
    tags = TagsSerializer(many=True)

    class Meta:
        model = Need
        fields = ['id', 'title', 'status', 'content', 'money',
                  'classify', 'location', 'user',
                  'images', 'video', 'create_time', 'update_time',
                  'address', 'tags', 'phone', 'receiving_code']

    def create(self, validated_data):
        return super(NeedSerializer, self).create(validated_data)
